package com.njym.java.security.admin.rest;

import com.njym.java.security.admin.biz.CourseCatalogueBiz;
import com.njym.java.security.admin.biz.PaymentRecordBiz;
import com.njym.java.security.admin.entity.CourseCatalogue;
import com.njym.java.security.admin.entity.PaymentRecordVo;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("courseCatalogue")
public class CourseCatalogueController extends BaseController<CourseCatalogueBiz, CourseCatalogue> {
	@Autowired
	private PaymentRecordBiz paymentRecordBiz;


	@RequestMapping(value = "/choose", method = RequestMethod.GET)
	@ResponseBody
	public Object list(String name) {
		Example example = new Example(CourseCatalogue.class);
		if (StringUtils.isNotBlank(name)) {
			example.createCriteria().andLike("courseName", "%" + name + "%");
		}
		List<CourseCatalogue> list = baseBiz.selectByExample(example);
		List<Map<String, Object>> brandList = new ArrayList<>(list.size());
		Map<String, Object> data = new HashMap<>(1);
		for (CourseCatalogue p : list) {
			Map<String, Object> b = new HashMap<>(2);
			b.put("value", p.getCourseName());
			b.put("id", p.getId());
			brandList.add(b);
		}
		data.put("catalogue", brandList);
		return ResponseUtil.ok(data);
	}

	@RequestMapping(value = "/queryByCourse", method = RequestMethod.GET)
	@ResponseBody
	public Object queryByCourse(@RequestParam Map<String, Object> params) throws ParseException {
		try {
			Integer queryType = Integer.parseInt(params.get("queryType").toString());
			Integer flag = Integer.parseInt(params.get("flag").toString());
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List<PaymentRecordVo> list;
			Date startDate;
			Date endDate;
			if (flag == 1) {	//有时间传入
				startDate = formatter.parse( params.get("startDate").toString() );
				endDate = formatter.parse( params.get("endDate").toString() );
			}else {		//没有时间传入
				startDate = new Date();
				endDate = new Date();
			}

			if (queryType == 0) { //总销售量
				list = paymentRecordBiz.getTotals(startDate,endDate,flag);
			}else{	//按代理细分
				list = paymentRecordBiz.getTotalsByPerson(startDate,endDate,flag);
			}

//			List<PaymentRecordVo> list;
//			if (radio.equals("季")) {
//				list = paymentRecordBiz.queryByQuater(courseId);
//			}else {
//				String timex = "";
//				if (radio.equals("月")) {
//					timex = "%Y-%m";
//				} else if (radio.equals("年")) {
//					timex = "%Y";
//				}
//				list =  paymentRecordBiz.queryByEx(courseId, timex);
//			}
			return new ObjectRestResponse<>().rel(true).data(list);
		}catch (RuntimeException e){
			return new ObjectRestResponse<>().rel(false);
		}

	}


	@RequestMapping(value = "/queryByPerson", method = RequestMethod.GET)
	@ResponseBody
	public Object queryByPerson(@RequestParam Map<String, Object> params){
		try {


			return new ObjectRestResponse<>().rel(true);
		}catch (RuntimeException e){
			return new ObjectRestResponse<>().rel(false);
		}
	}



}